package com.yiji.fintech.musfinance.common.utils;

import com.acooly.core.common.facade.ResultCode;
import com.acooly.core.utils.Money;
import com.acooly.core.utils.Strings;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.yiji.fintech.musfinance.common.exception.AbortBizException;
import org.springframework.util.Assert;

import java.util.Collection;

public class BizAssert {

    public static void notEmpty(Collection<?> collection,String description) {
        try {
            Assert.notEmpty(collection);
        } catch (Exception e) {
            throw new AbortBizException(description);
        }
    }

    public static void notEmpty(Collection<?> collection) {
        notEmpty(collection, null);
    }

    public static void isTrue(boolean expression,String description) {
        try {
            Assert.isTrue(expression);
        } catch (Exception e) {
            throw new AbortBizException(description);
        }
    }

    public static void isTrue(boolean expression) {
        isTrue(expression, null);
    }

    public static void isNull(Object object,String description) {
        try {
            Assert.isNull(object);
        } catch (Exception e) {
            throw new AbortBizException(description);
        }
    }

    public static void isNull(Object object) {
        isNull(object, null);
    }

    public static void notNull(Object object,String description) {
        try {
            Assert.notNull(object);
        } catch (Exception e) {
            throw new AbortBizException(description);
        }
    }

    public static void notEmpty(String str, ResultCode resultCode, String description){
        try {
           if(Strings.isEmpty(str)){
               throw new IllegalArgumentException(description);
           }
        } catch (Exception e) {
            if (StringUtils.isBlank(description)) {
                description = resultCode.getMessage();
            }
            throw new AbortBizException(description);
        }
    }

    public static void notNull(Object object) {
        notNull(object, null);
    }

    public static void equals(Object obj1, Object obj2) {
        equals(obj1, obj2, null);
    }

    public static void equals(Object obj1, Object obj2, String description) {
        if (!obj1.equals(obj2)) {
            throw new AbortBizException(description);
        }
    }

    /**
     * @param availableAmount 可用额度
     * @param amount          当前交易金额
     * @param description
     */
    public static void isLegalAmount(Money availableAmount, Money amount,String description) {
        try {
            isTrue(availableAmount.compareTo(amount) >= 0, null);
        } catch (Exception e) {
            throw new AbortBizException(description);// + ",当前可用金额为:" + availableAmount
        }
    }

    public static void isLegalAmount(Money availableAmount, Money amount) {
        isLegalAmount(availableAmount, amount, null);
    }
}